import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:connectivity_plus/connectivity_plus.dart';

import '../../providers/offline_mode_provider.dart';
import '../../../core/offline/offline_mode_manager.dart';

/// 离线模式状态显示Widget
class OfflineModeWidget extends ConsumerWidget {
  const OfflineModeWidget({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final offlineModeStatus = ref.watch(offlineModeStatusProvider);
    final connectivityStatus = ref.watch(connectivityStatusProvider);

    return Card(
      margin: const EdgeInsets.all(8.0),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min, // 添加mainAxisSize以避免无限高度
          children: [
            const SelectableText(
              '离线模式状态',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            _buildStatusRow(
                '连接状态', _connectivityStatusText(connectivityStatus)),
            _buildStatusRow('离线模式', _offlineModeStatusText(offlineModeStatus)),
            const SizedBox(height: 16),
            Row(
              children: [
                Expanded(
                  child: SizedBox(
                    // 添加SizedBox包装器
                    height: 36, // 设置固定高度
                    child: ElevatedButton(
                      onPressed: () {
                        // 检查Provider是否存在再调用
                        try {
                          ref
                              .read(offlineModeNotifierProvider.notifier)
                              .forceOffline();
                        } catch (e) {
                          // 静默处理错误
                        }
                      },
                      child: const SelectableText('强制离线'),
                    ),
                  ),
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: SizedBox(
                    // 添加SizedBox包装器
                    height: 36, // 设置固定高度
                    child: ElevatedButton(
                      onPressed: () {
                        // 检查Provider是否存在再调用
                        try {
                          ref
                              .read(offlineModeNotifierProvider.notifier)
                              .forceOnline();
                        } catch (e) {
                          // 静默处理错误
                        }
                      },
                      child: const SelectableText('强制在线'),
                    ),
                  ),
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: SizedBox(
                    // 添加SizedBox包装器
                    height: 36, // 设置固定高度
                    child: ElevatedButton(
                      onPressed: () {
                        // 检查Provider是否存在再调用
                        try {
                          ref
                              .read(offlineModeNotifierProvider.notifier)
                              .manualSync();
                        } catch (e) {
                          // 静默处理错误
                        }
                      },
                      child: const SelectableText('手动同步'),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  /// 构建状态行
  Widget _buildStatusRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        children: [
          SelectableText('$label: ',
              style: const TextStyle(fontWeight: FontWeight.w500)),
          SelectableText(value),
        ],
      ),
    );
  }

  /// 获取网络连接状态文本
  String _connectivityStatusText(AsyncValue<ConnectivityResult> status) {
    if (status is AsyncData<ConnectivityResult>) {
      switch (status.value) {
        case ConnectivityResult.wifi:
          return 'WiFi';
        case ConnectivityResult.mobile:
          return '移动数据';
        case ConnectivityResult.none:
          return '无连接';
        default:
          return '未知';
      }
    }
    return '加载中...';
  }

  /// 获取离线模式状态文本
  String _offlineModeStatusText(AsyncValue<OfflineModeStatus> status) {
    if (status is AsyncData<OfflineModeStatus>) {
      switch (status.value) {
        case OfflineModeStatus.online:
          return '在线模式';
        case OfflineModeStatus.offline:
          return '离线模式';
        case OfflineModeStatus.syncing:
          return '同步中...';
      }
    }
    return '加载中...';
  }
}
